package com.jian.mall.member.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.jian.mall.common.constant.UserConstant;
import com.jian.mall.common.redis.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.jian.mall.member.entity.MemberReceiveAddressEntity;
import com.jian.mall.member.service.MemberReceiveAddressService;
import com.jian.mall.common.utils.PageUtils;
import com.jian.mall.common.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * 会员收货地址
 *
 * @author tainanle
 * @email 12345678@gmail.com
 * @date 2022-08-11 09:44:44
 */
@RestController
@RequestMapping("member/memberreceiveaddress")
public class MemberReceiveAddressController {
    @Autowired
    private MemberReceiveAddressService memberReceiveAddressService;

    @GetMapping("/address/{userId}")
    public R addressByUserId(@PathVariable("userId")Long userId){
        List<MemberReceiveAddressEntity> entities =
                memberReceiveAddressService.addressByUserId(userId);
        return R.ok().put("address",entities);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:memberreceiveaddress:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberReceiveAddressService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:memberreceiveaddress:info")
    public R info(@PathVariable("id") Long id) {
        MemberReceiveAddressEntity memberReceiveAddress = memberReceiveAddressService.getById(id);

        return R.ok().put("memberReceiveAddress", memberReceiveAddress);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:memberreceiveaddress:save")
    public R save(@RequestBody MemberReceiveAddressEntity memberReceiveAddress,
                  HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(UserConstant.USER_LOGIN_STATUS);
        if (user != null && user.getId() != null) {
            memberReceiveAddress.setMemberId(user.getId());
            memberReceiveAddressService.save(memberReceiveAddress);
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:memberreceiveaddress:update")
    public R update(@RequestBody MemberReceiveAddressEntity memberReceiveAddress) {
        memberReceiveAddressService.updateById(memberReceiveAddress);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:memberreceiveaddress:delete")
    public R delete(@RequestBody Long[] ids) {
        memberReceiveAddressService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
